package gossip.v2.test;

import gossip.v2.GossipCore;
import gossip.v2.cluster.GossipNode;

public class GossipNodeExample {
    public static void main(String[] args) {
        // 创建本地节点
        GossipNode localNode = new GossipNode("node-1", "10.0.2.251", 9000);

        // 初始化核心
        GossipCore core = new GossipCore(localNode);

        // 添加种子节点
        core.getClusterManager().addSeedNode(new GossipNode("seed-1", "10.0.2.251", 9001));

        // 添加状态监听器
        core.getListeners().add(new GossipCore.GossipListener() {
            @Override
            public void onNodeJoined(GossipNode node) {
                System.out.println("Node joined: " + node.getNodeId());
            }

            @Override
            public void onNodeLeft(GossipNode node) {
                System.out.println("Node left: " + node.getNodeId());
            }

            @Override
            public void onStateChanged(String key, Object value) {
                System.out.println("State updated: " + key + " = " + value);
            }
        });

        // 启动集群
        core.start();

        // 更新状态
        core.updateState("config.version", "v2.1.0".getBytes());
        core.updateState("node.status", "active".getBytes());

        // 关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(core::stop));
    }
}